package com.example.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 * MAP 與 BEAN 轉換類
 * Project NAME：campin.primavera.unifier   
 * Class NAME：MapUtils   &
 * Author：Kangping Zhou
 * Date：Aug 15, 2019 8:35:08 AM      
 * Description：Kangpingchn@gmail.com
 * @version    
 *
 */
public class MapUtils {
	static Logger logger = Logger.getLogger(MapUtils.class);

	/**
	 * 从map集合中获取属性值
	 * 
	 * @param <E>
	 * @param map          map集合
	 * @param key          键对
	 * @param defaultValue 默认值
	 * @return
	 * @author jiqinlin
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public final static <E> E get(Map map, Object key, E defaultValue) {
		Object o = map.get(key);
		if (o == null)
			return defaultValue;
		return (E) o;
	}

	/**
	 * Map集合对象转化成 JavaBean集合对象
	 * 
	 * @param javaBean JavaBean实例对象
	 * @param list  Map数据集对象
	 * @return
	 * @author jqlin
	 */
	@SuppressWarnings({ "rawtypes" })
	public static <T> List<T> map2Java(Class clazz, List<Map<String,String>> list) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		List<T> objectList = new ArrayList<T>();

		T object = null;
		for (Map map : list) {
			if (map != null) {
				object = map2Java(clazz, map);
				objectList.add(object);
			}
		}
		return objectList;
	}
	

	/**
	 * Map对象转化成 JavaBean对象
	 * 
	 * @param javaBean JavaBean实例对象
	 * @param map      Map对象
	 * @return
	 * @author jqlin
	 */
	@SuppressWarnings({ "rawtypes", "unchecked", "hiding" })
	public static <T> T map2Java(Class clazz, Map map) {
		try {
			// 获取javaBean属性
			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
			// 创建 JavaBean 对象
			Object obj = clazz.newInstance();

			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				String propertyName = null; // javaBean属性名
				Object propertyValue = null; // javaBean属性值
				for (PropertyDescriptor pd : propertyDescriptors) {
					propertyName = pd.getName();
					if (map.containsKey(propertyName)) {
						propertyValue = map.get(propertyName);
//						System.out.println("property: "+propertyName+" : "+propertyValue);
						pd.getWriteMethod().invoke(obj, new Object[] { propertyValue });
					}
				}
				return (T) obj;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public static <T> List<Map> map2map(Class clazz, List<Map<String,String>> list) {
		return java2MapList(MapUtils.map2Java(clazz, list));
	}
	
	/**
	 * JavaBean对象转化成Map对象
	 * 
	 * @param javaBean
	 * @return
	 * @author
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map<String,Object> java2Map(Object javaBean) {
		Map map = new HashMap();
		try {
			// 获取javaBean属性
			BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());

			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			if (propertyDescriptors != null && propertyDescriptors.length > 0) {
				String propertyName = null; // javaBean属性名
				Object propertyValue = null; // javaBean属性值
				for (PropertyDescriptor pd : propertyDescriptors) {
					propertyName = pd.getName();
					if (!propertyName.equals("class")) {
						Method readMethod = pd.getReadMethod();
						propertyValue = readMethod.invoke(javaBean, new Object[0]);
						map.put(propertyName, propertyValue);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	

	/**
	 * JavaBean对象转化成Map对象
	 * 
	 * @param javaBean
	 * @return
	 * @author
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<Map> java2MapList(Object javaBeans) {
		List<Map> listmap = new ArrayList<Map>();
		Map map = null;
		BeanInfo beanInfo = null;
		try {
			for (Object javaBean : (List<Object>) javaBeans) {
				// 获取javaBean属性
				beanInfo = Introspector.getBeanInfo(javaBean.getClass());

				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
				if (propertyDescriptors != null && propertyDescriptors.length > 0) {
					map = new HashMap();
					String propertyName = null; // javaBean属性名
					Object propertyValue = null; // javaBean属性值
					for (PropertyDescriptor pd : propertyDescriptors) {
						propertyName = pd.getName();
//						System.out.println("propertyName: "+propertyName);
						
						if (!propertyName.equals("class")) {
							Method readMethod = pd.getReadMethod();
							propertyValue = readMethod.invoke(javaBean, new Object[0]);
//							logger.info("name: "+propertyName+": "+propertyValue);
							map.put(propertyName, propertyValue);
						}
					}
				}
				listmap.add(map);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return listmap;
	}


//	public static void main(String[] args) {
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("projid", "1211");
//		map.put("projcode", "Raining");
//		map.put("projname", "测试MAP名称");
//
//		Project proj = (Project) map2Java(Project.class, map);
//		System.out.println("projcode: " + proj.getProjcode());
//		System.out.println("projname: " + proj.getProjname());
//		System.out.println("projectid: "+proj.getProjid());
//
//	}
}